<!DOCTYPE html>
<html lang="en">
<head>
	<div th:include="base-libs-easyui :: base-libs-easyui" />
	<title>分级诊疗签约平台</title>
	<style>
		.datagrid-row-selected{
			background:#FFE4A5;
			color:#000000;
		}
	</style>
	<script th:inline="javascript">
        /**读取request中的初始化数据*/
        var menuAry = [[${roleInfo["MENUS"]}]];
        var userInfo = [[${userInfo}]];
	</script>
	<script type="text/javascript">
        var menuUrlObj = {}; // {"menuUrl":"menuName"...}
		var menuTree = createTreeList(menuAry.concat(), menuAry[0]["PRE_MENUID"]);
		var flg = true;
        $(function() {
			initView();
        });

        function initView()
		{
            createNav();
			$("#userName").html(userInfo.XM);
		    
		    $(".menu-tree").tree("collapseAll")  //折叠所有的菜单树
				.tree('expand',$('.menu-tree:first').tree('getRoot').target); //展开第一个子菜单

            //根据url子路径跳转页面 或者 默认查询第一个菜单
            selectOneMenu();
		}

		/**创建抽屉*/
		function createNav()
		{
			var ary = menuTree[0]["children"];
			$.each(ary, function(index, item){
			    /*创建抽屉*/
			    var acc = $($("#acc").html());
			    $(acc).attr("title", item["text"]);
			    /*创建树*/
			    var children = item["children"];
                var ul = $($(acc).find("ul"));
                $(ul).tree({
                    data: children
					,onSelect: function(node){
                        if(flg){
                            var curr = this;
							/*取消其他树的选中节点*/
                            $(".menu-tree").each(function(){
                                if(curr != this){
                                    $(this).find(".tree-node-selected").removeClass("tree-node-selected");
                                }
                            })
							/*url是否为空*/
                            var url = node["VIEWURL"];
                            if(StringUtils.isEmpty(url)){
								/*不能选中*/
                                $(this).find(".tree-node-selected").removeClass("tree-node-selected");
                            }else{
								/*是否与当前路径不同*/
                                var currUrl = $("#mainframe").attr("src");
                                if(currUrl != url){
                                    flg = false;
                                    $("#mainframe").attr("src", url);
                                }
                            }
						}
						/*单击菜单执行：展开/关闭 */
                        $(this).tree(node.state === 'closed' ? 'expand' : 'collapse', node.target);
                    }
                    ,onBeforeSelect : function (){
						return flg;
					}
				});
                $("#accordion").append(acc);
			})
            $("#accordion").accordion();
		}

        /**封装树形结构*/
        function createTreeList(list, sjjgdm) {
            var childList = [];
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                if(!$.isEmptyObject(item) && !StringUtils.isEmpty(item.VIEWURL)){
                    menuUrlObj[item.VIEWURL] = item.MENUNAME;
                }
                if (item == null) {
                    continue;
                }
                var sjjgdm1 = item["PRE_MENUID"];
                if (sjjgdm1 != sjjgdm) {
                    continue;
                } else {
                    var resid = item["MENUID"];
                    item["text"] = item["MENUNAME"];
                    item["iconCls"] = "";
                    var lt = createTreeList(list, resid);
                    if (lt != null && lt.length != 0) {
                        item["children"] = lt;
                    }
                    else{
                        if(StringUtils.isEmpty(item["VIEWURL"])){
                            item["selectable"] = false;
						}
					}
                    childList.push(item);
                    list[i] = null;
                }
            }
            if (childList.length == 0) {
                childList = null;
            }
            return childList;
        }

        /**修改密码回调*/
        function updatePwdHandler(){
            $.QuickAlert.alertIFrame({
				content: "ignore/updatePwd.html"
				,title: "修改密码"
				,shadeClose: false
				,width: "400px"
                ,height: "210px"
                ,cancel: function(index){
                    $.QuickAlert.alertConfirm({
                        content: "关闭窗口不会保存密码，是否关闭"
                        ,callback: function(){
                            parent.layer.closeAll();
                        }
                    });
                    return false;
                }
			})
		}

        function logoutHandler(){
            $.QuickUrlUtils.redirectUrl("ignore/login.html");
        }

        /** 选择一个菜单 */
        function selectOneMenu(){
            var urlReg = new RegExp("^.*from=(.+)$"),
                url = decodeURI(window.location.href),
                selectMenuName ;//选择的菜单的名称
//                alert(url)
            if(urlReg.test(url)){
                var sonUrl = url.replace(urlReg,"$1"); //访问路径中带的子路径
                var realUrl = sonUrl.split("?")[0];  //子路径的真实路径
                selectMenuName = menuUrlObj[realUrl];
                $("#mainframe").attr("src", sonUrl);
                selectMenuByHand(selectMenuName, true);
            }else{
                var firstMenu = $(".menu-tree div:first");
                selectMenuName = firstMenu.text();
                if(firstMenu.next().is("ul")){
                    selectMenuName = firstMenu.next().find("div").eq(0).text();
                }
                selectMenuByHand(selectMenuName);
            }
        }

        /** 手动选择一个菜单  */
        function selectMenuByHand(menuName/*菜单名称*/,justSelect/*只选中，不执行点击事件*/){
            $(".menu-tree div").each(function(i,item){
                var thisMenuName = $(this).text();
                if(thisMenuName == menuName){
                    if(justSelect){
                        $(this).addClass("tree-node-selected").parents("ul:first").css({"display":"block"})
                    }else{
                        $(this).trigger("click");
                    }
                    return false;
                }
            })
        }
	</script>
</head>

<body class="easyui-layout">
<div region="north"
	 style="height: 45px; background-color: #4a95e6; position: relative; overflow: hidden; border: none;">
	<div id="header" class="header-box">
		<div class="name">
			<span class="span-text" style="width: 420px;">分级诊疗签约平台</span>
		</div>
		<div class="right-tool">
			<ul>
				<li class="userInfo">
					<span>
						<span>你好！</span><span style="margin-right: 4px;" th:text="${userInfo.XM}">管理员</span></span>
					</span>
				</li>
				<li>
					<a target="_top" onclick="updatePwdHandler()">修改密码</a>
				</li>
				<li>
					<a target="_top" onclick="logoutHandler()">退出系统</a>
				</li>
			</ul>
		</div>
	</div>
</div>
<div region="center" style="border: none; overflow: hidden; background-color: #DEEDFB;">
	<div class="easyui-layout" fit="true">
		<div id="divWest" region="west" style="width: 210px; border: none; background-color: #DEEDFB; border-right: solid 1px #F9F9F9;">
			<div id="accordion" fit=true data-options="border:false">
			</div>
		</div>
		<div region="center" style="border: none; overflow: hidden; background-color: #FFF;">
			<iframe id="mainframe" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>
		</div>
	</div>
</div>
<script type="text/html" id="acc">
	<div title="TreeMenu" class="accMenu">
		<ul class="menu-tree"></ul>
	</div>
</script>
</body>
<script>
    var iframe = document.getElementById("mainframe");
    if (iframe.attachEvent){
        iframe.attachEvent("onload", function(){
//            alert("Local iframe is now sssloaded.");
            flg = true;
        });
    } else {
        iframe.onload = function(){
//            alert("Local iframe is now ddddloaded.");
            flg = true;
        };
    }


</script>
</html>
